package com.superpai.view.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.superpai.R;
import com.superpai.activity.CartActivity;
import com.superpai.domain.DBProduct;
import com.superpai.view.db.CartDBHelper;
import com.superpai.view.download.AsyncImageLoader;

/**
 * @author Devin.Hu
 * @date 2011-9-27
 * @version V1.0
 * @description 购物袋adapter
 */
public class CartAdapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater mInflater;
	private List<DBProduct> mList;
	private TextView productCartnum;
	private CartActivity mCartActivity;
	private ImageView cart_item_img;
	private TextView cart_item_title;
	private TextView cart_item_price;
	private TextView cart_item_size;
	private TextView cart_item_number;
	private TextView cart_item_delete;

	public CartAdapter(Context context, CartActivity cartActivity, TextView productCartnum) {
		this.mContext = context;
		mCartActivity = cartActivity;
		this.productCartnum = productCartnum;
		mInflater = LayoutInflater.from(mContext);
	}

	/**
	 * 设置购物袋数量
	 * 
	 * @param num
	 */
	public void setProductCartNum(Context mContext) {
		CartDBHelper db = new CartDBHelper(mContext);
		long num = db.getCount();
		if (num >= 0) {
			productCartnum.setVisibility(View.VISIBLE);
			productCartnum.setText(String.valueOf(num));
		} else {
			productCartnum.setVisibility(View.GONE);
		}
	}

	public List<DBProduct> getmList() {
		return mList;
	}

	public void setmList(List<DBProduct> mList) {
		this.mList = mList;
	}

	@Override
	public int getCount() {
		if (null != mList) {
			return mList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (null != mList) {
			return mList.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		convertView = mInflater.inflate(R.layout.cart_item, null);
		cart_item_img = (ImageView) convertView.findViewById(R.id.cart_item_img);
		cart_item_title = (TextView) convertView.findViewById(R.id.cart_item_title);
		cart_item_price = (TextView) convertView.findViewById(R.id.cart_item_price);
		cart_item_size = (TextView) convertView.findViewById(R.id.cart_item_size);
		cart_item_number = (TextView) convertView.findViewById(R.id.cart_item_number);
		cart_item_delete = (TextView) convertView.findViewById(R.id.cart_item_delete);

		cart_item_img.setTag(mList.get(position).getPimg());
		cart_item_title.setText(mList.get(position).getPname());
		cart_item_price.setText("￥" + mList.get(position).getPrice() + "元");
		cart_item_number.setText("数量：" + mList.get(position).getPnum());
		cart_item_size.setText("尺码：" + mList.get(position).getPnum());

		cart_item_delete.setTag(position);
		cart_item_delete.setOnClickListener(delList);

		new AsyncImageLoader().execute(cart_item_img);
		return convertView;
	}

	OnClickListener delList = new OnClickListener() {

		@Override
		public void onClick(View v) {
			
				final int position = Integer.parseInt(v.getTag().toString());
				
				Builder builder = new AlertDialog.Builder(mContext);
				builder.setTitle(mContext.getString(R.string.common_tishi));
				builder.setMessage(mContext.getString(R.string.common_cart_del));
				builder.setPositiveButton(mContext.getString(R.string.common_ok),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int whichButton) 
							{
								CartDBHelper db = new CartDBHelper(mContext);
								db.delItemByPcode(mList.get(position).getPcode());
								mList = db.findItemList();
								setProductCartNum(mContext);
								
								if (null != mList && mList.size() > 0) {
									mCartActivity.cart_productnum.setText(db.getCartItemNum(mList) + "");
									mCartActivity.cart_marketprice.setText(db.getCartMPrice(mList) + "");
									mCartActivity.cart_offer.setText(db.getOfferPrice(mList) + "");
									mCartActivity.cart_itemprice.setText(db.getCartTotalPrice(mList) + "");
									CartAdapter.this.setmList(mList);
									CartAdapter.this.notifyDataSetChanged();
								}else{
									mCartActivity.mNodata.setVisibility(View.VISIBLE);
								}
							}
						});
				builder.setNegativeButton(
						mContext.getString(R.string.common_cancel),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int whichButton) {
	
							}
						});
				builder.show();
			
		}
	};
}
